angular.module('BaseControl')

.directive("textareabox", [function() {
    return {
        restrict: "E",	// 指令是一个元素 (并非属性)，备选项有"A"、"E" 和 "C"， "M" ,分别代表attribute、element、class和comment（默认值为"A"）
        scope: {
            value: '=?',
            blur: '&',
            change: '&'
        },
        template: 		// 替换HTML (使用scope中的变量)
        '<textarea class="form-control BaseControl" style="z-index:9;position:relative;height: {{Height}};" oncontextmenu="return false;" ' +
        'ng-show="Visible" ng-disabled="!Enabled" ' +
        'ng-model="value" ng-click="click()" ng-focus="focus()" ng-blur="textBlur()"></textarea>',
        replace: true,			// 使用模板替换原始标记
        transclude: true,		// 不复制原始HTML内容
        controller: ['$scope', function($scope){
            $scope.click = function() {
                //$scope.$digest();
            };

            $scope.textChange = function() {
                $scope.change();
            };

            $scope.focus = function(){
                event.srcElement.select();
            };

            $scope.textBlur = function(){
                $scope.blur();
            };
        }],
        link: function (scope, element, attrs) {
            scope.Enabled = true;
            scope.Visible = true;
            scope.IsEmpty = true;
            scope.Height = "100%";
            scope.BindField = "";

            if (attrs["isempty"] != undefined && attrs["isempty"].toLocaleLowerCase() == "false") {
                scope.IsEmpty = false;
            }
            if (attrs["height"] != undefined && attrs["height"].trim() != "") {
                scope.Height = attrs["height"].trim();
            }
            if (attrs["bindfield"] != undefined && attrs["bindfield"].trim() != "") {
                scope.BindField = attrs["bindfield"].trim();
                if(scope.BindField.length > 0){
                    scope.$watch(
                        function(){
                            try{
                                return eval("scope.$parent." + scope.BindField);
                            }
                            catch(e){
                                return null;
                            }
                        },
                        function(){
                            var boxValue;
                            try{
                                boxValue = eval("scope.$parent." + scope.BindField);
                            }
                            catch(e){
                                boxValue = null;
                            }
                            if(boxValue != scope.value){
                                scope.value = boxValue;
                                window.setTimeout(function () {
                                    scope.textChange();
                                }, 1);
                            }
                        }
                    );
                }
            }

            element[0].addEventListener('change', function(){scope.textChange()}, false);

            var watch = scope.$watch(
                function(){
                    return attrs["enabled"];
                },
                function(){
                    if (attrs["enabled"] != undefined && attrs["enabled"].trim().toLocaleLowerCase() == "false") {
                        scope.Enabled = false;
                    }
                    else{
                        scope.Enabled = true;
                    }
                }
            );

            scope.$watch(
                function(){
                    return attrs["visible"]
                },
                function(){
                    if (attrs["visible"] != undefined && attrs["visible"].trim().toLocaleLowerCase() == "false") {
                        scope.Visible = false;
                    }
                    else{
                        scope.Visible = true;
                    }
                }
            );
            //watch() 注销监视
        }
    }
}]);
